package com.ruoyi.system.service.backend.impl;

import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.backend.*;
import com.ruoyi.system.domain.front.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.mapper.backend.SafetyExaminationBackMapper;
import com.ruoyi.system.mapper.front.MerchantFrontMapper;
import com.ruoyi.system.mapper.front.SafetyExaminationFrontMapper;
import com.ruoyi.system.service.backend.ISafetyExaminationBackService;
import com.ruoyi.system.service.front.IStudentFrontService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SafetyExaminationBackServiceImpl extends CxBase implements ISafetyExaminationBackService {

    @Autowired
    private IStudentFrontService studentFrontService;
    @Autowired
    private SafetyExaminationBackMapper safetyExaminationBackMapper;
    @Autowired
    private SafetyExaminationPaperquestionMapper safetyExaminationPaperquestionMapper;
    @Autowired
    private SafetyExaminationPaperInfoMapper safetyExaminationPaperInfoMapper;
    @Autowired
    private SafetyExaminationStudentRecordMapper safetyExaminationStudentRecordMapper;
    @Autowired
    private SafetyExaminationExaminationpaperMapper safetyExaminationExaminationpaperMapper;
    @Autowired
    private SafetyExaminationInfoMapper safetyExaminationInfoMapper;
    @Autowired
    private SafetyExaminationExaminationpaperMapper safetyExaminationPaperquestion;

    /**
     * 查询学院答题统计
     *
     * @param sysBackUsrInfo 查询信息
     * @return CollegeExamination
     */
    public List<CollegeExamination> selectCollegeExamination(SysBackUsrInfo sysBackUsrInfo){
        if(!checkNull(sysBackUsrInfo.getExaminationId())&&!checkNull(sysBackUsrInfo.getUserUnity())&&!checkNull(sysBackUsrInfo.getUserName())){
            List<CollegeExamination> collegeExaminations = new ArrayList<>();
            return safetyExaminationBackMapper.selectCollegeExamination(sysBackUsrInfo);
        }
        return null;
    }

    /**
     * 查询考试记录列表
     *
     * @param sysBackExStInfo 查询信息
     * @return StudentExamination
     */
    public List<StudentExamination> selectStudentExamination(SysBackExStInfo sysBackExStInfo){
        if(!checkNull(sysBackExStInfo.getCollegeValue())&&!checkNull(sysBackExStInfo.getExaminationId())){
            return safetyExaminationBackMapper.selectStudentExamination(sysBackExStInfo);
        }
        return null;
    }

    /**
     * 添加选择题目
     *
     * @param sysBackQSetInfo 题目信息
     * @return INT
     */
    public int insertPaperQuestion(SysBackQSetInfo sysBackQSetInfo){
        if(sysBackQSetInfo.getQuestion().size()>0&&!checkNull(sysBackQSetInfo.getExaminationPaperId())&&!checkNull(sysBackQSetInfo.getSetId())) {
            SafetyExaminationPaperInfo safetyExaminationPaperInfo = safetyExaminationPaperInfoMapper.selectSafetyExaminationPaperInfoById(sysBackQSetInfo.getExaminationPaperId());
            if (safetyExaminationPaperInfo != null) {
                SafetyExaminationPaperquestion safetyExaminationPaperquestion = new SafetyExaminationPaperquestion();
                safetyExaminationPaperquestion.setSafetyExaminationPaperId(sysBackQSetInfo.getExaminationPaperId());
                sysBackQSetInfo.setSetDate(getStamp().toString());
                List<SafetyExaminationPaperquestion> safetyExaminationPaperquestions = safetyExaminationPaperquestionMapper.selectSafetyExaminationPaperquestionList(safetyExaminationPaperquestion);
                int n = 0, m, s = 0;
                if(safetyExaminationPaperquestions.size()<=0){
                    for (; s < sysBackQSetInfo.getQuestion().size(); s++) {
                        sysBackQSetInfo.getQuestion().get(s).setQuestionSoft(Long.valueOf(s));
                    }
                }
                else {
                    for (int i = 0; i < safetyExaminationPaperquestions.size(); i++) {
                        //m为两个试卷顺序间的差值,s为循环到添加试卷的个数,n为比前一个顺序高出的位数
                        if (i == 0) {
                            m = n + safetyExaminationPaperquestions.get(i).getQuestionSoft().intValue();
                            if (safetyExaminationPaperquestions.get(i).getQuestionSoft() != 0) {
                                for (; s < sysBackQSetInfo.getQuestion().size() && s < m; s++, n++) {
                                    sysBackQSetInfo.getQuestion().get(s).setQuestionSoft(Long.valueOf(s));
                                }
                            } else if(safetyExaminationPaperquestions.get(i).getQuestionSoft() == 0 && safetyExaminationPaperquestions.size() == 1) {
                                for (n = 1; s < sysBackQSetInfo.getQuestion().size(); s++, n++) {
                                    sysBackQSetInfo.getQuestion().get(s).setQuestionSoft(Long.valueOf(n));
                                }
                            }
                        } else if (i > 0) {
                            m = safetyExaminationPaperquestions.get(i).getQuestionSoft().intValue() - safetyExaminationPaperquestions.get(i - 1).getQuestionSoft().intValue() - 1;
                            if(m>1) {
                                for (n = 1; s < sysBackQSetInfo.getQuestion().size(); n++, s++) {
                                    sysBackQSetInfo.getQuestion().get(s).setQuestionSoft(safetyExaminationPaperquestions.get(i - 1).getQuestionSoft() + n);
                                }
                            }
                            if (i == safetyExaminationPaperquestions.size() - 1) {
                                for (n = 1; s < sysBackQSetInfo.getQuestion().size(); s++, n++) {
                                    sysBackQSetInfo.getQuestion().get(s).setQuestionSoft(safetyExaminationPaperquestions.get(i).getQuestionSoft() + n);
                                }
                            }
                        }
                    }
                }
                safetyExaminationPaperInfo.setSafetyExaminationQuestionAmount(safetyExaminationPaperInfo.getSafetyExaminationQuestionAmount()+sysBackQSetInfo.getQuestion().size());
                safetyExaminationPaperInfoMapper.updateSafetyExaminationPaperInfo(safetyExaminationPaperInfo);
                return safetyExaminationBackMapper.insertPaperQuestion(sysBackQSetInfo);
            }
        }
        return 0;
    }

    /**
     * 添加选择试卷
     *
     * @param sysBackPSetInfo 题目信息
     * @return INT
     */
    public int insertExaminationPaper(SysBackPSetInfo sysBackPSetInfo){
        if(sysBackPSetInfo.getPaper().size()>0 &&!checkNull(sysBackPSetInfo.getSafetyExaminationId())&&!checkNull(sysBackPSetInfo.getSetId())) {
            SafetyExaminationInfo safetyExaminationInfo = safetyExaminationInfoMapper.selectSafetyExaminationInfoById(sysBackPSetInfo.getSafetyExaminationId());
            if (safetyExaminationInfo != null) {
                SafetyExaminationExaminationpaper safetyExaminationExaminationpaper = new SafetyExaminationExaminationpaper();
                safetyExaminationExaminationpaper.setSafetyExaminationId(sysBackPSetInfo.getSafetyExaminationId());
                sysBackPSetInfo.setSetDate(getStamp().toString());
                List<SafetyExaminationExaminationpaper> safetyExaminationExaminationpapers = safetyExaminationExaminationpaperMapper.selectSafetyExaminationExaminationpaperList(safetyExaminationExaminationpaper);
                int n = 0, m, s = 0;
                if(safetyExaminationExaminationpapers.size()<=0){
                    for (; s < sysBackPSetInfo.getPaper().size(); s++) {
                        sysBackPSetInfo.getPaper().get(s).setSafetyExaminationPaperSoft(Long.valueOf(s));
                    }
                }
                else {
                    for (int i = 0; i < safetyExaminationExaminationpapers.size(); i++) {
                        //m为两个试卷顺序间的差值,s为循环到添加试卷的个数,n为比前一个顺序高出的位数
                        if (i == 0) {
                            m = n + safetyExaminationExaminationpapers.get(i).getSafetyExaminationPaperSoft().intValue();
                            if (safetyExaminationExaminationpapers.get(i).getSafetyExaminationPaperSoft() != 0) {
                                for (; s < sysBackPSetInfo.getPaper().size() && s < m; s++, n++) {
                                    sysBackPSetInfo.getPaper().get(s).setSafetyExaminationPaperSoft(Long.valueOf(s));
                                }
                            } else if(safetyExaminationExaminationpapers.get(i).getSafetyExaminationPaperSoft() == 0 && safetyExaminationExaminationpapers.size() == 1) {
                                for (n = 1; s < sysBackPSetInfo.getPaper().size(); s++, n++) {
                                    sysBackPSetInfo.getPaper().get(s).setSafetyExaminationPaperSoft(Long.valueOf(n));
                                }
                            }
                        } else if (i > 0) {
                            m = safetyExaminationExaminationpapers.get(i).getSafetyExaminationPaperSoft().intValue() - safetyExaminationExaminationpapers.get(i - 1).getSafetyExaminationPaperSoft().intValue() - 1;
                            if(m>1) {
                                for (n = 1; s < sysBackPSetInfo.getPaper().size(); n++, s++) {
                                    sysBackPSetInfo.getPaper().get(s).setSafetyExaminationPaperSoft(safetyExaminationExaminationpapers.get(i - 1).getSafetyExaminationPaperSoft() + n);
                                }
                            }
                            if (i == safetyExaminationExaminationpapers.size() - 1) {
                                for (n = 1; s < sysBackPSetInfo.getPaper().size(); s++, n++) {
                                    sysBackPSetInfo.getPaper().get(s).setSafetyExaminationPaperSoft(safetyExaminationExaminationpapers.get(i).getSafetyExaminationPaperSoft() + n);
                                }
                            }
                        }
                    }
                }
                SafetyExaminationPaperInfo safetyExaminationPaperInfo = new SafetyExaminationPaperInfo();
                List<SafetyExaminationPaperInfo> safetyExaminationPaperInfos = new ArrayList<>();
                for(SysBackPSoft paperid:sysBackPSetInfo.getPaper()){
                    safetyExaminationPaperInfo.setSafetyExaminationPaperId(paperid.getSafetyExaminationPaperId());
                    safetyExaminationPaperInfos = safetyExaminationPaperInfoMapper.selectSafetyExaminationPaperInfoList(safetyExaminationPaperInfo);
                    safetyExaminationPaperInfos.get(0).setSafetyExaminationUse(safetyExaminationPaperInfos.get(0).getSafetyExaminationUse()+1);
                    safetyExaminationPaperInfoMapper.updateSafetyExaminationPaperInfo(safetyExaminationPaperInfos.get(0));
                }
                return safetyExaminationBackMapper.insertExaminationPaper(sysBackPSetInfo);
            }
        }
        return 0;
    }

}
